﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class FormReiseNum : System.Windows.Forms.Form
	{
		public FormReiseNum(FormReiseDrawVerticalPipe form)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.form = form;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.SetUserData();
			this.form.District = this.cbDistrict.Checked;
			this.form.Build = this.cbBuild.Checked;
			this.form.DistrictNum = this.GetDefaultTxtValue(this.hyTextBox3.Text);
			this.form.BuildNum = this.GetDefaultTxtValue(this.hyTextBox4.Text);
			base.Close();
		}

		private void FormReiseAuxiliaryFrame_Load(object sender, EventArgs e)
		{
			this.GetUserData();
		}

		private void GetUserData()
		{
			this.cbDistrict.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "District") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "District")));
			this.cbBuild.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "Build") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "Build")));
			this.hyTextBox3.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "DistrictNum") == "") ? "A" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "DistrictNum"));
			this.hyTextBox4.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "BuildNum") == "") ? "1" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "BuildNum"));
		}

		private void SetUserData()
		{
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "District", this.cbDistrict.Checked.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "Build", this.cbBuild.Checked.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "DistrictNum", this.GetDefaultTxtValue(this.hyTextBox3.Text));
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "BuildNum", this.GetDefaultTxtValue(this.hyTextBox4.Text));
		}

		private string GetDefaultTxtValue(string txtValue)
		{
			if (txtValue == "" || !Regex.IsMatch(txtValue, "^[0-9a-zA-Z'_]+$"))
			{
				txtValue = "1";
			}
			return txtValue;
		}

		private void SetTextEnable(bool bIsEnable)
		{
			this.hyTextBox3.Enabled = bIsEnable;
			this.hyTextBox4.Enabled = bIsEnable;
		}

		private void hyTextBox3_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = (YJKTextBox)sender;
			if (!this.RegexText(hytextBox.Text))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				hytextBox.Focus();
				hytextBox.SelectAll();
				return;
			}
		}

		private bool RegexText(string strText)
		{
			return Regex.IsMatch(strText, "^[0-9a-zA-Z'_]+$");
		}

		private void cbDistrict_CheckedChanged(object sender, EventArgs e)
		{
			if (((CheckBox)sender).Checked)
			{
				this.hyTextBox3.Enabled = true;
				return;
			}
			this.hyTextBox3.Enabled = false;
		}

		private void cbBuild_CheckedChanged(object sender, EventArgs e)
		{
			if (((CheckBox)sender).Checked)
			{
				this.hyTextBox4.Enabled = true;
				return;
			}
			this.hyTextBox4.Enabled = false;
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private FormReiseDrawVerticalPipe form;
	}
}
